package easy;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author lzy
 * @create 2020-05-15 22:32
 */
public class AddOne {
    public int[] plusOne(int[] digits) {
        if (digits.length == 1) {
            if (digits[0] == 9) {
                return new int[]{1, 0};
            }
            return new int[]{digits[0] + 1};
        }

        int headout = (digits[digits.length - 1] + 1) > 9 ? 1 : 0;
        for (int i = digits.length - 2; i > 0; i--) {
            headout = (headout + digits[i]) > 9 ? 1 : 0;
            digits[i] = (headout + digits[i]) % 10;
        }
        if (headout == 1) {
            int[] result = new int[digits.length + 1];
            result[0] = 1;
            for (int i = 1; i < result.length; i++) {
                result[i] = digits[i - 1];
            }
            return result;
        }

        return digits;
    }
}
